<script>
	import {
		version,
		name
	} from './package.json'
	import {
		versionName
	} from '@/manifest.json'
	// #ifdef APP-PLUS
	import appUpgrade from '@/common/appUpgrade.js';
    import NotifiUtil from '@/common/notification.js'
	// #endif
	export default {
		globalData: {
		    //im 相关配置 开始
		    package_name:'chat', //组件名
		    appconfig:{}, //后台配置
            pushversion:1,//unipush版本
            pushcid:'',//推送标识
            conversation_id:'',//当前聊天标识
		    //im 相关配置 结束
	    },
		onLaunch: function() {
            this.$fc.getConfig();
			// #ifdef H5
				uni.setStorageSync('device', 'H5');
				uni.setStorageSync('version', versionName);
			// #endif
            if(this.globalData.pushversion==2){
                uni.onPushMessage((msg) => {
        			this.$fc.dopushmsg(msg);
        		})
            }else{
                // #ifdef APP-PLUS
					var msg={};
					uni.onPushMessage((res) => {
						if(res.type){
							var content= JSON.parse(res.data.content);
							msg.type = res.type;msg.data={};
							msg.data.title = content.title;
							msg.data.content = content.body;
							msg.data.payload = res.data.payload?res.data.payload:content.payload;
						}else{
							msg.type = 'click';msg.data={};
							msg.data.title = res.title;
							msg.data.content = res.body;
							msg.data.payload = res.payload;
						}
						this.$fc.dopushmsg(msg);
					})
					plus.push.addEventListener('click', res => {
						if(res.type){
							var content= JSON.parse(res.content);
							msg.type = 'click';msg.data={};
							msg.data.title = content.title;
							msg.data.content = content.body;
							msg.data.payload = res.payload?res.payload:content.payload;
						}else{
							msg.type = 'click';msg.data={};
							msg.data.title = res.title;
							msg.data.content = res.body;
							msg.data.payload = res.payload;
						}
						this.$fc.dopushmsg(msg);
					});
        		// #endif
            }
			let token = uni.getStorageSync('Authorization');
			if (!token) {
				// #ifdef APP-PLUS
					plus.navigator.closeSplashscreen();
				// #endif
			} else {
				uni.reLaunch({
					url: '/'+this.globalData.package_name+"/index/index",
				}).then(res=>{
					// #ifdef APP-PLUS
					plus.navigator.closeSplashscreen();
					// #endif
				})
			}
			// #ifdef APP-PLUS
				//升级检测
				uni.getSystemInfo({
					success: (res)=> {
						uni.setStorageSync('device', res.platform);
						plus.runtime.getProperty(plus.runtime.appid, (widgetInfo)=> {
							uni.setStorageSync('version', widgetInfo.version);
							this.$http.request({
								url: '/common/getVersion',
								success: (res) => {
									if(res.data.data.upgrade=='Y'){
										appUpgrade.init({
											titleText: '版本更新'+res.data.data.version,
											packageUrl:res.data.data.url,
											content: res.data.data.content,
											forceUpgrade:res.data.data.forceUpgrade=='Y' ? true : false
										});
										appUpgrade.show();
									}
								}
							});
						});
					}
				});
				uni.onNetworkStatusChange( (res)=> {
					if(res.isConnected){
						this.$store.dispatch('get_UserInfo')
					}
				});
				var appname = uni.getSystemInfoSync().appName;
				var NotificationUtil = NotifiUtil();//实例创建
				NotificationUtil.clearNotification(1000);
				NotificationUtil.setNotification(appname, appname+"应用正在运行中");
			// #endif
		},
		onShow: function() {
			// #ifdef APP-PLUS
				var args = plus.runtime.arguments;
				if(args){
					var userInfo = this.$store.state.userInfo;
					if(userInfo)return '';//console.log(args);
					// 处理args参数，如直达到某新页面等
					this.$store.dispatch('get_UserInfo').then(res=>{
						userInfo = this.$store.state.userInfo;
					}).catch((error)=>{
						var token = this.$fc.url_query(args,'token');
						this.$fc.loginwithtoken(token);
					});
				}
			// #endif
		},
		methods: {
		   
		}
	}
</script>
<style lang="scss">
	/*每个页面公共css */
	@import '@/uni_modules/uni-scss/index.scss';
	@import "@/static/styles/animation.css";
	/* #ifndef APP-NVUE */
	@import '@/static/customicons.css';
	// 设置整个项目的背景色
		page {
			box-sizing: border-box;
		}
	/* #endif */
</style>
